package com.ylkj.patient.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

import com.ylkj.patient.MainApplication;

public class SearchSpUtils {
    private static SearchSpUtils instance = new SearchSpUtils();
    private static SharedPreferences mSp;
    private Context mContext;

    private SearchSpUtils() {
    }


    public static SearchSpUtils getInstance() {
        initSpUtils(MainApplication.getContext());
        return instance;
    }

//    初始化

    public static void initSpUtils(Context context) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("hytx_SearchHistory", Context.MODE_PRIVATE);
        }
    }

    //保存

    public void save(String key, Object value) {
        if (value instanceof String) {
            mSp.edit().putString(key, (String) value).commit();
        } else if (value instanceof Integer) {
            mSp.edit().putInt(key, (Integer) value).commit();
        } else if (value instanceof Boolean) {
            mSp.edit().putBoolean(key, (Boolean) value).commit();
        } else if (value instanceof Long) {
            mSp.edit().putLong(key, (Long) value).commit();
        }
    }

    //取
    public String getString(String key, String defValue) {
        return mSp.getString(key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mSp.getBoolean(key, defValue);
    }

    public int getInteger(String key, int defValue) {
        return mSp.getInt(key, defValue);
    }

    public long getLong(String key, long defValue) {
        return mSp.getLong(key, defValue);
    }

    public boolean removeData(@NonNull String[] keys) {
        for (int i = 0; i < keys.length; i++) {
            mSp.edit().remove(keys[i]);
        }
        mSp.edit().apply();
        return true;
    }

    @SuppressLint("CommitPrefEdits")
    public boolean removeAll(Context context) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.clear();
        edit.commit();
        return true;
    }

    public boolean removekey(String key) {
        SharedPreferences.Editor edit = mSp.edit();
        edit.remove(key);
        edit.commit();
        return true;
    }
}
